import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/net/login_connect.dart';
import 'package:kkhousekeep/page/login/reset_pwd_page.dart';
import 'package:kkhousekeep/tools/string_ext.dart';
import 'package:kkhousekeep/tools/toast_util.dart';

import '../../net/respnse_code.dart';

class ForgetPwdController extends GetxController {
  late TextEditingController phoneController;
  late TextEditingController codeController;

  Timer _timer = Timer(const Duration(minutes: 0), () {});

  ///按钮置灰
  var isButtonAsh = true.obs;

  ///页面显示状态
  var isInputPhonePage = true.obs;

  ///文本倒计时文本
  var countDown = 60.obs;

  ///能否发送验证码
  var canSendCode = false.obs;

  ///转换后的手机号码
  var processedPhone = "".obs;

  ///实际的手机号码
  late String phone;
  late LoginConnect loginConnect;

  @override
  void onInit() {
    _initTextController();
    loginConnect = Get.find<LoginConnect>();
    super.onInit();
  }

  @override
  void onClose() {
    if (_timer.isActive) {
      _timer.cancel();
    }
    phoneController.dispose();
    super.onClose();
  }

  void _initTextController() {
    phoneController = TextEditingController();
    codeController = TextEditingController();
  }

  ///获取验证码
  void getCode() async {
    if (!canSendCode.value) {
      return;
    }

    if (isInputPhonePage.value && !phoneController.text.checkCNPhone()) {
      ToastUtils.showToast(tip: "请输入正确的手机号码");
      return;
    }
    //手机号转换
    if (isInputPhonePage.value) {
      processedPhone.value = phoneController.text.replaceRange(3, 7, "****");
      phone = phoneController.text;
    }
    var response = await loginConnect.resetPwdFirstStep(phone);
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      isInputPhonePage.value = false;
      phoneController.text = "";
      codeController.text = "";
      isButtonAsh.value = true;
      canSendCode.value = false;
      _startCountDown();
      return;
    }
  }

  /// 重置密码页可返回到为“初始状态”的忘记密码页
  void buildResetPwdPage() async {
    var response =
        await loginConnect.resetPwdSecondStep(phone, codeController.text);
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      processedPhone.value = "";
      codeController.text = "";
      isButtonAsh.value = true;
      canSendCode.value = false;
      isInputPhonePage.value = true;
      countDown.value = 60;
      Get.to(() => ResetPwdPage(
            phone: phone,
          ));
    }
    if (response.code == ResponseCode.errorCode) {
      ToastUtils.showToast(tip: "验证码错误");
      return;
    }
  }

  ///倒计时
  void _startCountDown() {
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      countDown--;
      if (countDown.value == 0) {
        timer.cancel();
        canSendCode.value = true;
        countDown.value = 60;
      }
    });
  }

  ///观察文本
  void observerText() {
    isButtonAsh.value = true;
    if (!isInputPhonePage.value && codeController.text.length == 6) {
      isButtonAsh.value = false;
      return;
    }
    if (isInputPhonePage.value && phoneController.text.length == 11) {
      canSendCode.value = true;
      isButtonAsh.value = false;
      return;
    }
  }
}
